package com.tboss.template.common.excelStyle;

import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.tboss.global.helpers.excel.ExcelDomain;
import com.tboss.global.helpers.excel.StyleBody;

public class BlueBody implements StyleBody {

	@Override
	public void setBody(Workbook workbook, ExcelDomain<?> domain) {
		// TODO Auto-generated method stub
		CellStyle cellStyle = this.getCellStyle(workbook);//Applying Style of Sheet
		this.setFont(workbook, cellStyle);//Applying Font of Sheet 
		this.setBodyRow(workbook, domain, cellStyle);//Writing rows on sheet
	}
	
	private CellStyle getCellStyle(Workbook workbook){
		CellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
		cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
		cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
		cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
		cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
		cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
		cellStyle.setBorderRight(CellStyle.BORDER_THIN);
		cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
		cellStyle.setBorderTop(CellStyle.BORDER_THIN);
		cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
		cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
		
		return cellStyle;
	}
	
	private void setFont(Workbook workbook, CellStyle cellStyle){
		Font font = workbook.createFont();		
		font.setFontHeightInPoints((short)9);
		font.setBoldweight(Font.BOLDWEIGHT_NORMAL);
		font.setColor(IndexedColors.WHITE.getIndex());
		cellStyle.setFont(font);
	}
	
	private void setBodyRow(Workbook workbook, ExcelDomain<?> domain, CellStyle cellStyle){
		Sheet sheet = workbook.getSheet(domain.getSheetName());
		
		int rIdx = 1;
		for (Object obj : domain.getValues()) {			
			//Column Value Height
			Row body = sheet.createRow(rIdx++);
			body.setHeight((short)350);
			
			int cIdx = 0;
			for(Object o : (List<?>) obj){
				Cell bCell = body.createCell(cIdx++);
				
				if(o instanceof String){
					bCell.setCellValue((String) o);
					bCell.setCellStyle(cellStyle);
				}
			}
		}
	}
	
}
